<?php defined('SYSPATH') or die('No direct script access.');
/**
 * URL Helper for Multi_Lang module
 *
 * @package    Multi_Lang Module 
 * @author     Kiall Mac Innes
 * @copyright  (c) 2007-2008 Multi_Lang Module Team..
 * @license    http://www.gnu.org/licenses/gpl-2.0.html
 */
class url extends url_Core {
	/**
	 * Fetches an absolute site URL based on a URI segment.
	 *
	 * @param   string  site URI to convert
	 * @param   string  non-default protocol
	 * @return  string
	 */
	public static function site($uri = '', $protocol = FALSE)
	{
		if ($path = trim(parse_url($uri, PHP_URL_PATH), '/'))
		{
			// Add path suffix
			$path .= Config::item('core.url_suffix');
		}

		if ($query = parse_url($uri, PHP_URL_QUERY))
		{
			// ?query=string
			$query = '?'.$query;
		}

		if ($fragment = parse_url($uri, PHP_URL_FRAGMENT))
		{
			// #fragment
			$fragment =  '#'.$fragment;
		}
		
		// Check if language is already in URL, else add the current language.
		
		$lang = '';
		
		if (Config::item('multi_lang.enabled')) {
			$lang = Router::$language.'/';
		}
		// Concat the URL
		return url::base(TRUE, $protocol).$lang.$path.$query.$fragment;
	}
	
	/**
	 * Fetches an absolute site URL based on a URI segment and supplied language.
	 *
	 * @param   string  language code to use. eg en/fr/es etc
	 * @param   string  site URI to convert
	 * @param   string  non-default protocol
	 * @return  string
	 */
	public static function site_lang($lang, $uri = '', $protocol = FALSE)
	{
		if ($path = trim(parse_url($uri, PHP_URL_PATH), '/'))
		{
			// Add path suffix
			$path .= Config::item('core.url_suffix');
		}

		if ($query = parse_url($uri, PHP_URL_QUERY))
		{
			// ?query=string
			$query = '?'.$query;
		}

		if ($fragment = parse_url($uri, PHP_URL_FRAGMENT))
		{
			// #fragment
			$fragment =  '#'.$fragment;
		}
		
		if (Config::item('multi_lang.enabled')) {
			$lang .= '/';
		} else {
			// Wipe out the lang as the url will be invalid without multi_lang enabled.
			$lang = '';
		}
		
		// Concat the URL
		return url::base(TRUE, $protocol).$lang.$path.$query.$fragment;
	}
	
	/**
	 * Fetches the current URI.
	 *
	 * @param   boolean  include the query string
	 * @return  string
	 */
	public static function current($qs = FALSE)
	{
		$lang = '';
		
		if (Config::item('multi_lang.enabled')) {
			$lang = Router::$language.'/';
		}
		
		if ($qs === TRUE) {
			return $lang.Router::$complete_uri;
		} else {
			return $lang.Router::$current_uri;
		}
	}
	
	/**
	 * Fetches the current URI using a supplied language.
	 *
	 * @param   string	language code to use. eg en/fr/es etc
	 * @param   boolean	include the query string
	 * @return  string
	 */
	public static function current_lang($lang, $qs = FALSE)
	{		
		if (Config::item('multi_lang.enabled')) {
			$lang .= '/';
		} else {
			// Wipe out the lang as the url will be invalid without multi_lang enabled.
			$lang = '';
		}
		
		if ($qs === TRUE) {
			return $lang.Router::$complete_uri;
		} else {
			return $lang.Router::$current_uri;
		}
	}
	
} // End url